$import("com.joypard.platform.organization.org.OrgDI");
/**
 * 组织机构组件
 */
K.defView("com.joypard.platform.organization.org.SelectOrgCompoent", {
	/** ------ ------ ------ ------ 静态属性及方法定义 ------ ------ ------ ------ */
	statics : {
		/**
		 * 创建一个组织机构下拉树
		 */
		createOrgTreeCombo : function(config) {
			var options = K.extend({
				checkModel : 'single'
			}, config);
			options.root = options.root || OrgDI.getRootOrgInfo(); // 请放心这样写不会额外的去加载数据
			options.loader = options.loader || OrgView.createOrgTreeLoader();
			return new Ext.ux.TreeCombo(options);
		},
		/**
		 * 获取组织机构树加载器
		 */
		createOrgTreeLoader : function() {
			var loader = new K.Ext.tree.TreeLoader({
				url : OrgDI.urls.getOrgChildrens,
				requestMethod : "GET",
				baseParams : {
					parentOrgId : "0"
				},
				listeners : {
					beforeload : function(loader, node) {
						this.baseParams.parentOrgId = node.id;
					}
				},
				filterTreeData : function(obj) {
					var output = K.createOutput(obj);
					return output.getParameter("orgs");
				},
				createNode : function(attr) {
					attr.text = attr.name;
					return K.Ext.tree.TreeLoader.prototype.createNode.call(this, attr);
				}
			});
			return loader;
		},

		/**
		 * 创建组织机构树
		 * 
		 * @param config
		 * @returns {K.Ext.tree.TreePanel}
		 */
		createOrgTreePanel : function(config) {
			var options = {
				title : "组织机构树",
				frame : true,
				iconCls : "p-tab",
				autoScroll : true,
				region : "west",
				margins : "1 0 0 0",
				width : "20%"
			};
			K.extend(options, config);
			options.loader = this.createOrgTreeLoader();
			var treePanel = new K.Ext.tree.TreePanel(options);
			treePanel.setRootNode(this.createOrgAsyncRootNode());
			return treePanel;
		},

		/**
		 * 创建一个异步根组织信息
		 * 
		 * @param rootCode
		 * @returns {K.Ext.tree.AsyncTreeNode}
		 */
		createOrgAsyncRootNode : function(rootCode) {
			var rootInfo = OrgDI.getRootOrgInfo(rootCode, false);
			rootInfo.text = rootInfo.name;
			rootInfo.leaf = rootInfo.leaf == 1;
			return new K.Ext.tree.AsyncTreeNode(rootInfo);
		}
	}
});